package com.fancy.job;

import com.fancy.dao.FancyMusicUserDao;
import com.fancy.pojo.UserListenNumber;
import com.fancy.service.ListenNumberService;
import com.fancy.service.impl.ListenNumberServiceImpl;
//import com.fancy.utils.RedisSynchronizationMysql;
import com.fancy.utils.SimpleDateFormatUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.Date;
import java.util.List;
import java.util.Set;

//任务类
public class Quartz implements Job {
    @Autowired
    private SimpleDateFormatUtil simpleDateFormatUtil;
//    @Autowired
//    private RedisSynchronizationMysql redisSynchronizationMysql;
//    @Autowired
//    private ListenNumberService listenNumberService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
/*
    @Autowired
    private FancyUserListenNumberDao fancyUserListenNumberDao;*/

    //Integer zsetKey = 1;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
         System.out.println(simpleDateFormatUtil.DateFormat(new Date()));
        //System.out.println(redisSynchronizationMysql.readDate());
        //System.out.println(writeDate());
//        readDate();

    }

    public static int dateSynchoronization(int a, int b) {
        return a + b;
    }

    public boolean writeDate() {
        /*Set<String> keys = redisTemplate.keys("ranking-*");
        for (String zsetKey : keys
        ) {
            Integer userId = Integer.parseInt(zsetKey.split("_")[1]);
            Cursor<ZSetOperations.TypedTuple<String>> cursor = redisTemplate.opsForZSet().scan(zsetKey, ScanOptions.NONE);
            while (cursor.hasNext()) {
                ZSetOperations.TypedTuple<String> item = cursor.next();
                String uln = item.getValue();
                String[] strArr = uln.split(",");
                //Integer userId = Integer.parseInt(strArr.)
                Integer musicId = Integer.parseInt(strArr[0]);
                String UserName = strArr[1];
                String musicName = strArr[2];
                String singerName = strArr[3];
                Double numberOfListening = item.getScore();
                //UserListenNumber userListenNumber = new UserListenNumber(null, 1,musidId,);
                UserListenNumber userListenNumber = new UserListenNumber();
                if (fancyUserListenNumberDao.getListenNumber(1, musicId) != null) {
                    fancyUserListenNumberDao.updateUserListenNumber(userListenNumber);
                } else {
                    System.out.println(fancyUserListenNumberDao.insertUserListenNumber(userListenNumber));
                }
                return true;
            }
        }*/
        return false;
    }
}

